package com.gxuwz.drivemaster;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.gxuwz.drivemaster.R;

public class ExamineFragment extends Fragment {
    private Fragment subjectOneFragment = new SubjectOneFragment();

    private String[] titles = {"科目一","科目三","科目四", };

    private TabLayout tab_laout;
    private ViewPager2 viewPager;


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 加载子 Fragment 到 R.id.fragment_container

        tab_laout = view.findViewById(R.id.tab_layout);
        viewPager = view.findViewById(R.id.viewPager);

        //viewPager 需要设置一个adapter
        viewPager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                String title = titles[position];
                switch(title) {
                    case "科目一":
                        return new SubjectOneFragment();
                    case "科目二":
                        return new SubjectTwoFragment();
                    case "科目三":
                        return new SubjectThreeFragment();
                    case "科目四":
                        return new SubjectFourFragment();
                    default:
                        return TabNewsFragment.newInstance(title);
                }
            }

            @Override
            public int getItemCount() {
                return titles.length;
            }
        });

        //tab_laout点击事件
        tab_laout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //设置viewPager选中当前页
                viewPager.setCurrentItem(tab.getPosition(), false);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        //tab_layout和viewPager关联在一起
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tab_laout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(titles[position]);
            }
        });

        tabLayoutMediator.attach();

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_examine, container, false);
        return v;
    }
}
